package com.weike.wk_android.utils;

import android.util.Log;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public abstract class BaseObserver<T> implements Observer<BaseResponse<T>> {
    private static final String TAG = "BaseObserver";

    @Override
    public void onSubscribe(Disposable d) {
        Log.i(TAG, "onSubscribe: ");
    }

    @Override
    public void onNext(BaseResponse<T> response) {
        // 机出数据统一处理
        if (response.getRes_code() >= 200 && response.getRes_code() < 300){
            onSuccess(response.getT());
        } else {
            onFailure(null, response.getErr_msg());
        }
    }

    protected abstract void onSuccess(T t);
    protected abstract void onFailure(Throwable e, String err_mag);

    @Override
    public void onComplete() {
        Log.i(TAG, "onComplete: ");
    }

    @Override
    public void onError(Throwable e) {
        Log.i(TAG, "onError: " + e.getMessage());
        onFailure(e, RxExceptionUtil.exceptionHandler(e));
    }
}
